package org.example.qqrobot.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.qqrobot.context.BaseContext;
import org.example.qqrobot.mapper.*;
import org.example.qqrobot.model.dto.ProductNewDTO;
import org.example.qqrobot.model.dto.ProductQueryDTO;
import org.example.qqrobot.model.pojo.Collection;
import org.example.qqrobot.model.pojo.Label;
import org.example.qqrobot.model.pojo.Message;
import org.example.qqrobot.model.pojo.Product;
import org.example.qqrobot.model.vo.ProductIntoPageVO;
import org.example.qqrobot.model.vo.ProductMainVO;
import org.example.qqrobot.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductLabelMapper productLabelMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public List getList(ProductQueryDTO productQueryDTO) {

        //获得一个数组
         List<ProductMainVO> pvList = new ArrayList<>();

        //原始的数据
        List<Product> pList = productMapper.query(productQueryDTO);



        for (int i = 0; i < pList.size(); i++) {
              ProductMainVO productMainVo = new ProductMainVO();
              BeanUtils.copyProperties(pList.get(i),productMainVo);
              productMainVo.setName(userMapper.getNameById(pList.get(i).getWriter()));
              productMainVo.setLabelList(productLabelMapper.findByProductId(pList.get(i).getId()));
              pvList.add(productMainVo);

        }

        return pvList;

    }

    @Override
    public ProductIntoPageVO intoPage(Long id) {
        ProductIntoPageVO productIntoPageVO = new ProductIntoPageVO();

        Product product =  productMapper.findById(id);
        BeanUtils.copyProperties(product,productIntoPageVO);
        productIntoPageVO.setName(userMapper.getNameById(product.getWriter()));
        return productIntoPageVO;
    }

    @Override
    public void save(String title,Integer price,String introduction,String image) {
        Product product = new Product();
        product.setImage(image);
        product.setTitle(title);
        product.setIntroduction(introduction);
        product.setPrice(price);
        product.setWriter(BaseContext.getCurrentId());
        product.setCreateTime(LocalDateTime.now());
        productMapper.save(product);



    }

    @Override
    public void collect(Long id) {
        List<Collection> cList = collectMapper.findAllByUserId(BaseContext.getCurrentId());
        for (int i = 0; i < cList.size(); i++) {
            if(cList.get(i).getProductId() == id){
                return;
            }
        }

        collectMapper.save(id, BaseContext.getCurrentId());
    }

    @Override
    public List<ProductMainVO> getTheCollection() {
        List<Long> productList = collectMapper.findByUserId(BaseContext.getCurrentId());

        List<ProductMainVO> productMainVOList = new ArrayList<>();
        //获取product
        for (int i = 0; i < productList.size(); i++) {
            Product product = productMapper.findById(productList.get(i));
            ProductMainVO productMainVO = new ProductMainVO();
            if(product != null) {
                BeanUtils.copyProperties(product, productMainVO);
            }

            //List<Label> lList = productLabelMapper.findByProductId(productList.get(i).getId());
            productMainVO.setName(userMapper.getNameById(productMapper.findById(productList.get(i)).getWriter()));

            //productMainVO.setLabelList(lList);

            productMainVOList.add(productMainVO);


        }

        return productMainVOList;
    }

    @Override
    public List<ProductMainVO> getMyProduct() {

        List<Product> pList = productMapper.findByWriterId(BaseContext.getCurrentId());
        List<ProductMainVO> pmList = new ArrayList<>();
        //获取product
        for (int i = 0; i < pList.size(); i++) {

            ProductMainVO productMainVO = new ProductMainVO();
            BeanUtils.copyProperties(pList.get(i),productMainVO);

            List<Label> lList = productLabelMapper.findByProductId(pList.get(i).getId());

            productMainVO.setLabelList(lList);
            productMainVO.setName(userMapper.getNameById(pList.get(i).getWriter()));

            pmList.add(productMainVO);


        }

        return pmList;


    }


    @Override
    public void sendMessage(String message, Long id) {
        Message message1 = new Message();
        message1.setContent(message);
        message1.setSendTime(LocalDateTime.now());
        message1.setSender(BaseContext.getCurrentId());
        message1.setStatus(1);
        message1.setReceiver(id);
        messageMapper.save(message1);
        

    }

    @Override
    public List<ProductMainVO> query(String queryName) {
        //获得一个数组
        List<ProductMainVO> pvList = new ArrayList<>();

        //原始的数据
        List<Product> pList = productMapper.queryByName(queryName);



        for (int i = 0; i < pList.size(); i++) {
            ProductMainVO productMainVo = new ProductMainVO();
            BeanUtils.copyProperties(pList.get(i),productMainVo);
            productMainVo.setName(userMapper.getNameById(pList.get(i).getWriter()));
            productMainVo.setLabelList(productLabelMapper.findByProductId(pList.get(i).getId()));
            pvList.add(productMainVo);

        }

        return pvList;

    }
}
